package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version: 1.0
 * @Author: 李佳毅
 * @description: 套餐控制器
 * @date: 2025-07-07 22:10
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    //新增套餐
    @PostMapping
    @ApiOperation("新增套餐")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐: {}", setmealDTO);
        boolean result = setmealService.saveWithDish(setmealDTO);
        return result ? Result.success() : Result.error("新增套餐失败");
    }

    //分页查询
    @GetMapping("/page")
    @ApiOperation("套餐分页查询")
    public Result pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询: {}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.pageQuery(setmealPageQueryDTO);
        return pageResult != null ? Result.success(pageResult) : Result.error("分页查询失败");
    }

    //根据ids批量删除套餐
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除套餐: {}", ids);
        boolean result = setmealService.deleteByIds(ids);
        return result ? Result.success() : Result.error("批量删除失败");
    }

    //根据id查询套餐信息,用于修改回显
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐信息")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("根据id查询套餐信息: {}", id);
        SetmealVO setmealVO = setmealService.findById(id);
        return setmealVO != null ? Result.success(setmealVO) : Result.error("查询失败");
    }

    //修改套餐信息
    @PutMapping
    @ApiOperation("修改套餐信息")
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐信息: {}", setmealDTO);
        boolean result = setmealService.updateWithDish(setmealDTO);
        return result ? Result.success() : Result.error("修改套餐信息失败");
    }

    //修改套餐的起售状态
    @PostMapping("status/{status}")
    @ApiOperation("修改套餐的售卖状态")
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("修改套餐的起售状态: {}, {}", status, id);
        boolean result = setmealService.startOrStop(status, id);
        return result ? Result.success() : Result.error("修改套餐的售卖状态失败");
    }
}
